package com.yanqu.road.server.cmd.tradewar;

import com.yanqu.road.entity.enums.eTradeWarBattleType;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.tradewar.UserTradeWarBattle;
import com.yanqu.road.logic.bussiness.player.UserPatronsBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PatronsPb;
import com.yanqu.road.pb.patrons.PatronsProto;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.ForbiddenMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.config.TradeWarConfigMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.entity.tradewar.UserTradeWarInfo;
import com.yanqu.road.pb.tradewar.TradeWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.TradeWarModule;
import com.yanqu.road.server.pb.TradeWarBattlePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_TRADE_WAR_BATTLE_PREPARE, desc = "商战上阵准备")
public class TradeWarBattlePrepareCmd extends AbstractCommand {

	private int battleType;
	private List<Integer> patronsIdList;
	private long enemyUserId;
	private String battleUid;
	private int useItemId;
	private int warTimes = 1;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		TradeWarProto.TradeWarBattleReqMsg reqMsg = TradeWarProto.TradeWarBattleReqMsg.parseFrom(packet.getBytes());
		battleType = reqMsg.getBattleType();
		patronsIdList = new ArrayList<>();
		for(Integer patronsId : reqMsg.getPatronsIdListList()){
			if(!patronsIdList.contains(patronsId)){
				patronsIdList.add(patronsId);
			}
		}
		enemyUserId = reqMsg.getEnemyUserId();
		battleUid = reqMsg.getBattleUid();
		useItemId = reqMsg.getUseItemId();
		if(reqMsg.hasWarTimes() && reqMsg.getWarTimes() > 0){
			warTimes = reqMsg.getWarTimes();
		}
		if(warTimes > 3){
			warTimes = 3;
		}
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(player.getTitleId() < GameConfig.TRADE_WAR_ONCE_MULTIPLE_NEED_TITLE && warTimes > 1){   //身份限制
			ret = GameErrorCode.E_TITLE_LEVEL_NO_ENOUGH;
			return;
		}

		if (patronsIdList.size() == 0 || patronsIdList.size() > GameConfig.TRADE_WAR_MAX_BATTLE_PATRONS_COUNT) {
			ret = GameErrorCode.E_TRADE_WAR_PATRONS_COUNT_ERROR;
			return;
		}

		if(eTradeWarBattleType.Track.getValue() == battleType) {
			if (ForbiddenMgr.isForbiddenCmd(player.getUserId(), Protocol.S_CROSS_TRADE_WAR_BATTLE_PREPARE)) {
				ret = GameErrorCode.E_SYSTEM_NO_OPEN;
				return;
			}
		}


		ret = player.getModule(TradeWarModule.class).prepare(battleType, patronsIdList, enemyUserId, battleUid, useItemId, warTimes);
	}

	@Override
	public void sendData(GamePlayer player) {
		TradeWarProto.TradeWarBattleRespMsg.Builder respMsg = TradeWarProto.TradeWarBattleRespMsg.newBuilder();
		respMsg.setRet(ret);
		long score = 0;
		UserTradeWarInfo userTradeWarInfo = player.getModule(TradeWarModule.class).getUserTradeWarInfo();
		if(null != userTradeWarInfo){
			score = userTradeWarInfo.getScore();
		}
		UserTradeWarBattle userTradeWarBattle = player.getModule(TradeWarModule.class).getUserTradeWarBattle();
		TradeWarProto.TradeWarBattleTempMsg.Builder battleMsg = TradeWarBattlePb.parseTradeWarBattleTempMsg(userTradeWarBattle, score);
		if(null != battleMsg) {
			respMsg.setBattleInfo(battleMsg);
		}
		if(0 == ret && null != userTradeWarBattle){
			List<UserPatrons> patronsList;
			BigInteger enemyAbility = BigInteger.ZERO;
			if(userTradeWarBattle.getEnemyUserId() > 0){
				GamePlayer enemyPlayer = GamePlayerMgr.getOnlinePlayer(userTradeWarBattle.getEnemyUserId());
				if(null != enemyPlayer){
					patronsList = new ArrayList<>(enemyPlayer.getModule(PatronsModule.class).getUserPatronsMap().values());
				}else {
					patronsList = new ArrayList<>(UserPatronsBussiness.getUserPatronsMap(userTradeWarBattle.getEnemyUserId()).values());
				}
			}else {
				patronsList = TradeWarConfigMgr.initNpcPatronList(new ArrayList<>(player.getModule(PatronsModule.class).getUserPatronsMap().values()));
			}
			enemyAbility = PatronsMgr.getPatronsListAbility(patronsList);
			for(UserPatrons userPatrons : patronsList){
				PatronsProto.PatronsTempMsg.Builder patronsMsg = PatronsPb.parsePatronsTempMsg(userPatrons);
				respMsg.addEnemyPatronsList(patronsMsg);
			}
			respMsg.setEnemyPatronsAbility(enemyAbility.toString());
		}
		player.sendPacket(Protocol.U_TRADE_WAR_BATTLE_PREPARE, respMsg);
	}

}
